package com.weilele.mvvm.utils.local;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;

import com.github.jokar.multilanguages.library.MultiLanguage;

import java.util.Locale;

public class LocalManageUtil {

    private static final String TAG = "LocalManageUtil";

    /**
     * 获取系统的locale
     *
     * @return Locale对象
     */
    private static Locale getCurrentSystemLocale(Context context) {
        return MultiLanguage.getSystemLocal(context);
    }


    /**
     * 保存并更新语言
     *
     * @param selectLanguage
     * @param context
     * @param locale
     */
    private static void saveCurrentSelectLocaleToSp(String selectLanguage, Context context, Locale locale) {
        if (SPUtil.getInstance(context).saveCurrentSelectLocalToSp(selectLanguage)) {
            SPUtil.getInstance(context).setCurrentSelectLocal(locale);
            MultiLanguage.setApplicationLanguage(context);
        }
    }

    /**
     * 保存当前选择的语言
     *
     * @param context
     * @param selectLanguage
     */
    public static void saveCurrentSelectLocale(Context context, Locale selectLanguage) {
        if (selectLanguage == null) {
            return;
        }
        saveCurrentSelectLocaleToSp(selectLanguage.toString(), context, selectLanguage);
    }

    /**
     * 保存当前选择的语言
     *
     * @param context
     * @param selectLanguage
     */
    public static void saveCurrentSelectLocale(Context context, String selectLanguage) {
        if (selectLanguage == null) {
            return;
        }
        Locale local = SPUtil.getInstance(context).getLocalByString(selectLanguage);
        saveCurrentSelectLocaleToSp(selectLanguage, context, local);
    }

    /**
     * 获取选择的语言设置
     *
     * @param context
     * @return
     */
    public static Locale getCurrentSelectLocale(Context context) {
        return SPUtil.getInstance(context).getCurrentSelectLocal(context);
    }

    /**
     * 国际化操作
     */
    public static Context attachBaseContext(Context base) {
        return MultiLanguage.setLocal(base);
    }

    /**
     * 国际化操作
     */
    public static void onConfigurationChanged(Application app, Configuration newConfig) {
        //如果设置了当前选择的系统语言，切换语言的时候就不跟随系统改变
        //如果不论什么时候都想跟所系统改变，去除这个判断
        if (SPUtil.getInstance(app).getCurrentSelectLocalFromSp() != null) {
            return;
        }
        //最新更改的local，需要保存到sp
        Locale systemLocal = MultiLanguage.getSystemLocal(newConfig);
        //保存到sp
        saveCurrentSelectLocale(app, systemLocal);
        MultiLanguage.onConfigurationChanged(app.getApplicationContext());
    }


    private static class SPUtil {
        private Locale currentSelectLocal;
        private final String TAG_LANGUAGE = "language_select";
        private static volatile SPUtil instance;
        private final SharedPreferences mSharedPreferences;


        private SPUtil(Context context) {
            String SP_NAME = "language_setting";
            mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        }

        /**
         * 选择的语言保存到sp
         *
         * @param select
         */
        private boolean saveCurrentSelectLocalToSp(String select) {
            SharedPreferences.Editor edit = mSharedPreferences.edit();
            edit.putString(TAG_LANGUAGE, select);
            return edit.commit();
        }

        /**
         * 从sp获取保存的语言
         *
         * @return
         */
        private String getCurrentSelectLocalFromSp() {
            return mSharedPreferences.getString(TAG_LANGUAGE, null);
        }


        private static SPUtil getInstance(Context context) {
            if (instance == null) {
                synchronized (SPUtil.class) {
                    if (instance == null) {
                        instance = new SPUtil(context);
                    }
                }
            }
            return instance;
        }

        /**
         * 获取当前app选择的语言
         *
         * @param context
         * @return
         */
        private Locale getCurrentSelectLocal(Context context) {
            if (currentSelectLocal == null) {
                String localFromSp = getCurrentSelectLocalFromSp();
                if (localFromSp == null) {
                    currentSelectLocal = MultiLanguage.getSystemLocal(context);
                } else {
                    currentSelectLocal = getLocalByString(localFromSp);
                }
            }
            return currentSelectLocal;
        }

        private void setCurrentSelectLocal(Locale currentSelectLocal) {
            this.currentSelectLocal = currentSelectLocal;
        }

        private Locale getLocalByString(String selectLanguage) {
            Locale locale;
            if (selectLanguage.contains("_")) {
                String[] split = selectLanguage.split("_");
                locale = new Locale(split[0], split[1]);
            } else {
                locale = new Locale(selectLanguage);
            }
            return locale;
        }
    }
}